$(function () {
    var userInfo = sessionStorage['users'];
    var userName;
    if (userInfo == null) {
        alert('请登录！');
        location.href = 'login.html';
    } else {
        userName = JSON.parse(sessionStorage['users']);
        $('#userName').html(userName[0].Name);
    }
    var allUser = JSON.parse(localStorage['userInfos']);

    for (let i = 0; i < allUser.length; i++) {
        if (userName[0].Name == allUser[i].userName) {
            var userReaName = allUser[i].sjName;
            var useraddress = allUser[i].address;
            var userReaPhone = allUser[i].phoneNum;
            if (userReaName == null || useraddress == null || userReaPhone == null) {
                alert('请完善个人信息！')
                location.href = 'place.html'
            } else {
                $('.userName').html('收货人：' + userReaName);
                $('.userAddress').html('收货地址：' + useraddress);
                $('.userTel').html("收件人手机号：" + userReaPhone);
            }
        }
    }

    $.get('data/goods.json', function (response) {
        var data = response;
        var num = {};
        var allGoods = JSON.parse(sessionStorage['buyGood']);
        var strMes = [];
        for (let i = 0; i < allGoods.length; i++) {
            var flag = true;
            var t = allGoods[i].goods.title;
            for (key in num) {
                if (key == t) {
                    num[key]++
                    flag = false
                }
            }
            if (flag) {
                num[t] = '1'
            }
        }

        for (var k = 0; k < data.goods.length; k++) {
            for (key in num) {
                if (data.goods[k].title == key) {

                            strMes += `<ul>
                                <li><span class="message">
                                <img class="goodPic" src="${data.goods[k].imgPath}" alt="">
                                </img>
                                <strong>${key}</strong>
                                </span>
                                </li>
                                <li><span class="price">${data.goods[k].price}</span></li>
                                <li><span class="num">${num[key]}</span></li>
                                <li><span class="sum">${data.goods[k].price * num[key]}</span></li>
                            </ul>`
                            console.log(strMes)

                        
                    
                }
            }
        } $('.allMes').html(strMes)
    })

})

window.onload = function () {
    document.querySelector('.cancel').onclick = function () {
        location.href = document.referrer;
    }
    document.querySelector('.pay').onclick = function () {
        location.href = 'order.html';
    }

    
}